#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <wait.h>

void FunSonTask( int iSigNo )
{
    printf( "Good Boy Doing Homework\n");
    sleep( 3 );
    exit( 0 );
}

void FunFatherTask( int iSigNo )
{
    printf( "Father Happy\n" );
}

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    if ( pid > 0 )
    {
        int i = 0;
        for ( i = 0; i < 8; i++ )
        {
            printf( "Father Working\n");
            sleep( 1 );
        }
        printf( "Father Going Home\n" );
        kill( pid, SIGUSR1 );
        signal( SIGCHLD, FunFatherTask );
        wait( NULL );
        exit( 0 );

    }
    else if ( pid == 0 )
    {
        signal( SIGUSR1, FunSonTask );
        while ( 1 )
        {
            printf( "Bear Kid Playing Game ~~@!double kill#$~ Victory! ~%%~|?<>{}[]!@!@$^&*  :)\n" );
            sleep( 1 );
        }
    }
    else
    {
        perror( "fail fork" );
        return -1;
    }

    return 0;
}



/*
功能:
    父进程创建子进程
    父进程和子进程各自处理自己的任务
    父进程向子进程发送一个信号, 使得子进程执行新任务, 收到回复信号则等待子进程退出, 父进程退出
    子进程收到信号之后执行新任务, 新任务完成之后向父进程发送回复信号, 子进程退出.

函数:
    fork
    kill
    signal
    wait
*/